﻿///////////////////////////////////////////////////////////////////////////////
// File:        Log.cc
// Author:      671643387@qq.com
// Date:        2016年1月18日 上午11:34:25
// Description:
///////////////////////////////////////////////////////////////////////////////

#include "Log.h"

#include <boost/log/common.hpp>  
#include <boost/log/expressions.hpp>  
#include <boost/log/utility/setup/file.hpp>  
#include <boost/log/utility/setup/console.hpp>  
#include <boost/log/utility/setup/common_attributes.hpp>  
#include <boost/log/attributes/timer.hpp>  
#include <boost/log/attributes/named_scope.hpp>   
#include <boost/log/support/date_time.hpp>

namespace logging = ::boost::log;  
namespace sinks = ::boost::log::sinks;  
namespace attrs = ::boost::log::attributes;    
namespace expr = ::boost::log::expressions;  
namespace keywords = ::boost::log::keywords;

////
LogPtr gLog;

void Log::New(void)
{
    assert(!gLog);
    gLog.reset(new Log());
}

void Log::Free(void)
{
    gLog.reset();
}

Log::Log(void)
{
    logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%");

    logging::add_file_log  
        (  
        "info.log",  
        keywords::filter = expr::attr< eLogSeverityLevel >("Severity") <= kInfo,  
        keywords::format = expr::stream  
        << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")  
        << " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")  
        << "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")  
        << "] <" << expr::attr< eLogSeverityLevel >("Severity")  
        << "> " << expr::message
        )->locked_backend()->auto_flush(true);

    logging::add_file_log  
        (  
        "error.log",  
        keywords::filter = expr::attr< eLogSeverityLevel >("Severity") == kError,  
        keywords::format = expr::stream  
        << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")  
        << " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")  
        << "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")  
        << "] <" << expr::attr< eLogSeverityLevel >("Severity")  
        << "> " << expr::message
        )->locked_backend()->auto_flush(true);

    logging::add_file_log  
        (  
        "critical.log",  
        keywords::filter = expr::attr< eLogSeverityLevel >("Severity") == kCritical,  
        keywords::format = expr::stream  
        << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")  
        << " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")  
        << "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")  
        << "] <" << expr::attr< eLogSeverityLevel >("Severity")  
        << "> " << expr::message
        )->locked_backend()->auto_flush(true);

    logging::add_common_attributes();  
    logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());

    BOOST_LOG_FUNCTION();

    src_.reset(new ::boost::log::sources::severity_logger<eLogSeverityLevel>());
}

Log::~Log(void)
{
}

void Log::Debug(const char *format, ...)
{
    char log[kMaxLogLength];
    std::va_list varg;
    va_start(varg, format);
    int ret = vsnprintf(log, kMaxLogLength, format, varg);
    va_end(varg);

    MWOW_DEBUG << log;
}

void Log::Info(const char *format, ...)
{
    char log[kMaxLogLength];
    std::va_list varg;
    va_start(varg, format);
    int ret = vsnprintf(log, kMaxLogLength, format, varg);
    va_end(varg);

    MWOW_INFO << log;
}

void Log::Error(const char *format, ...)
{
    char log[kMaxLogLength];
    std::va_list varg;
    va_start(varg, format);
    int ret = vsnprintf(log, kMaxLogLength, format, varg);
    va_end(varg);

    MWOW_ERROR << log;
}

void Log::Critical(const char *format, ...)
{
    char log[kMaxLogLength];
    std::va_list varg;
    va_start(varg, format);
    int ret = vsnprintf(log, kMaxLogLength, format, varg);
    va_end(varg);

    MWOW_CRITICAL << log;
}